﻿package mortal.game.view.task.drama
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.effectEditTool.manager.*;
    import mortal.common.net.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.processor.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.friendBless.view.*;
    import mortal.game.view.funcPreView.*;
    import mortal.game.view.task.drama.interfaces.*;
    import mortal.game.view.task.drama.operations.*;
    import mortal.mvc.core.*;

    public class TaskDramaOperator extends Object
    {
        private var _lastStep:TTaskDrama;
        private var _curStepers:Dictionary;
        private var _runingSteper:ITaskDramaStepCommand;
        private var _checkDeadTimer:Timer;
        private var _lastOpTime:int = 0;
        private var _lastPoint:Point;
        private var _paths:Array;
        private var _heights:Array;
        private var _resumeCallback:Function = null;
        private var _needResumeNum:int = 0;
        private var _resumedNum:int = 0;
        private var _endTimerId:int;
        private var _blackScreen:TaskDramaOpBlackScreen;
        private var _blackThenTips:TaskDramaOpBlackScreenThenTips;
        private var _npcTalk:TaskDramaOpTalk;
        private var _hidePet:TaskDramaOpPetHide;
        private var _playerMoveOnly:TaskDramaPlayerMoveOnly;
        private var _moveScreen:TaskDramaScreenMove;
        private var _taskTalk:TaskDramaOpTaskTalkText;
        private var _playerScreenMove:TaskDramaPlayerScreenMove;
        private var _bossTalk:TaskDramaBossTalk;
        private var _bossAction:TaskDramaBossAction;
        private var _npcSceneTalk:TaskDramaNpcTalk;
        private var _npcAction:TaskDramaNpcAction;
        private var _movie:TaskDramaMovie;
        private var _effect:TaskDramaEffect;
        private var _monsterScreen:TaskDramaMonsterScreenCenter;
        private var _blowFly:TaskDramaBlowFly;
        public static var isInFlyDrama:Boolean = false;
        public static var ationTime:int = 0;

        public function TaskDramaOperator()
        {
            this._curStepers = new Dictionary();
            this._lastPoint = new Point();
            return;
        }// end function

        public function get checkDeadTimer() : Timer
        {
            if (this._checkDeadTimer == null)
            {
                this._checkDeadTimer = new Timer(20000);
                this._checkDeadTimer.addEventListener(TimerEvent.TIMER, this.onChecker);
            }
            return this._checkDeadTimer;
        }// end function

        private function onChecker(param1) : void
        {
            if (isInFlyDrama)
            {
                return;
            }
            if (FrameManager.flashFrame.getTime() - this._lastOpTime >= 24000)
            {
                Log.error("剧情，TimerChecker20秒自动结束了剧情");
                Dispatcher.dispatchEvent(new DataEvent(EventName.Task_DramaStopAll));
            }
            return;
        }// end function

        public function get lastStep() : TTaskDrama
        {
            return this._lastStep;
        }// end function

        public function stageResizeHandler() : void
        {
            if (this._npcTalk != null)
            {
                this._npcTalk.onStageResize();
            }
            if (this._movie != null)
            {
                this._movie.onStageResize();
            }
            return;
        }// end function

        private function takeFlyHeightsAction(param1:Array) : void
        {
            if (!isInFlyDrama)
            {
                return;
            }
            if (param1 == null || param1.length < 3)
            {
                return;
            }
            var _loc_2:* = param1.shift();
            var _loc_3:* = param1.shift();
            var _loc_4:* = param1.shift();
            Log.debug("fly飞行调试", _loc_2, _loc_3, _loc_4);
            Cache.instance.role.roleEntityInfo.updateTestSpeed(_loc_2);
            GameController.scene.flyToSky(_loc_4, _loc_3, true);
            setTimeout(this.takeFlyHeightsAction, _loc_4 * 1000, param1);
            return;
        }// end function

        public function flyToSky(param1:Array, param2:Array) : void
        {
            var finishFly:Function;
            var path:* = param1;
            var heights:* = param2;
            finishFly = function () : void
            {
                _paths = null;
                _heights = null;
                GameProxy.task.endFly();
                unLockAll();
                isInFlyDrama = false;
                GameController.scene.land(0, true);
                Cache.instance.role.roleEntityInfo.updateTestSpeed(0);
                return;
            }// end function
            ;
            if (path == null || heights == null)
            {
                return;
            }
            this._paths = path;
            this._heights = heights;
            this._lastPoint.x = RolePlayer.instance.x2d;
            this._lastPoint.y = RolePlayer.instance.y2d;
            isInFlyDrama = true;
            lockAll();
            this.takeFlyHeightsAction(heights);
            var arr:Array;
            var p:* = new AstarTurnPoint();
            p._x = Math.ceil(RolePlayer.instance.x2d / GameMapUtil.tileWidth);
            p._y = Math.ceil(RolePlayer.instance.y2d / GameMapUtil.tileHeight);
            arr.push(p);
            var i:int;
            while (i < path.length)
            {
                
                p = new AstarTurnPoint();
                p._x = Math.ceil(int(path[i]) / GameMapUtil.tileWidth);
                p._y = Math.ceil(int(path[(i + 1)]) / GameMapUtil.tileHeight);
                arr.push(p);
                i = i + 2;
            }
            path = MapGridToPixels.work(arr);
            MapGridToTargetPoint.work(path, 0.5 * GameMapUtil.tileWidth + p._x, 0.5 * GameMapUtil.tileHeight + p._y);
            AIManager.cancelAll();
            AIManager.addMoveByPath(path, 0, true);
            AIManager.addCallback(finishFly);
            AIManager.start();
            return;
        }// end function

        public function operate(param1:TTaskDrama, param2:Object = null) : void
        {
            this._lastOpTime = FrameManager.flashFrame.getTime();
            if (this._endTimerId > 0)
            {
                clearTimeout(this._endTimerId);
                this._endTimerId = -1;
            }
            this._lastStep = param1;
            ationTime = 0;
            if (this._lastStep.conditionEntity > 0 && this._lastStep.conditionType == EStepCondition._EStepConditionTimeout)
            {
                ationTime = this._lastStep.conditionEntity;
            }
            this._runingSteper = null;
            switch(param1.type)
            {
                case EStepType._EStepTypeNormal:
                {
                    lockAll();
                    AIManager.cancelAll();
                    Cache.instance.task.isDrama = true;
                    this.checkDeadTimer.start();
                    break;
                }
                case EStepType._EStepTypeFinish:
                {
                    Log.debug("剧情结束， time=" + getTimer());
                    Cache.instance.task.isDrama = false;
                    unLockAll();
                    if (this._npcTalk != null && !this._npcTalk.isHide)
                    {
                        this.npcTalk.cancel(param1);
                    }
                    this.checkDeadTimer.stop();
                    break;
                }
                case EStepType._EStepTypeDramaAdd:
                {
                    Log.debug("剧情+黑色背景， time=" + getTimer());
                    (this.npcTalk as TaskDramaOpTalk).showBlackBgs(this.callEnd);
                    this._runingSteper = this.npcTalk;
                    break;
                }
                case EStepType._EStepTypeDramaPrint:
                {
                    this._runingSteper = this.npcTalk;
                    this.npcTalk.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeDramaCancel:
                {
                    this.npcTalk.cancel(param1, this.callEnd);
                    Log.debug("剧情移除黑色背景， time=" + getTimer());
                    break;
                }
                case EStepType._EStepTypeTaskDramaPrint:
                {
                    this.taskTalk.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeAnimation:
                {
                    this._runingSteper = this.blackScreen;
                    this._curStepers[param1.type] = this._runingSteper;
                    this.blackScreen.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeAnimationEnd:
                {
                    this.blackScreen.cancel(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeAnimationPrint:
                {
                    this._runingSteper = this.blackThenTips;
                    this.blackThenTips.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypePetActive:
                {
                    this.hidePet.cancel(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypePetIdle:
                {
                    this.hidePet.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeEffect:
                {
                    this.effect.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeEffectActive:
                {
                    this.effect.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeEffectDel:
                {
                    this.effect.cancel(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypePlayerMoveOnly:
                {
                    this._curStepers[param1.type] = this.playerMoveOnly;
                    this.playerMoveOnly.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypePlayerMoveOnlyDel:
                {
                    this.playerMoveOnly.cancel(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeScreenMove:
                {
                    this._curStepers[param1.type] = this.moveScreen;
                    this.moveScreen.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeScreenRestore:
                {
                    this.moveScreen.cancel(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypePlayerMoveScreen:
                {
                    this.playerScreenMove.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeScreenLock:
                {
                    this.callEnd();
                    break;
                }
                case EStepType._EStepTypeScreenUnlock:
                {
                    this.callEnd();
                    break;
                }
                case EStepType._EStepTypePlayerFly:
                {
                    break;
                }
                case EStepType._EStepTypeMenuShow:
                {
                    this.callEnd();
                    break;
                }
                case EStepType._EStepTypeMenuHide:
                {
                    this.callEnd();
                    break;
                }
                case EStepType._EStepTypeMoviePlay:
                {
                    this._runingSteper = this.movie;
                    this.movie.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeMovieEnd:
                {
                    this.movie.cancel(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeBossDialog:
                {
                    this.bossTalk.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeBossActive:
                {
                    this.bossAction.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeNpcDialog:
                {
                    this.npcSceneTalk.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeNpcActive:
                {
                    this.npcAction.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeMonsterScreenCenter:
                {
                    this._curStepers[param1.type] = this.monsterScreen;
                    this.monsterScreen.call(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeUnLockMonsterScreenCenter:
                {
                    this.monsterScreen.cancel(param1, this.callEnd);
                    break;
                }
                case EStepType._EStepTypeShakingScreen:
                {
                    Game.scene.gameControler.checkShake(Game3DControler.defaultShake, 0);
                    this.callEnd();
                    break;
                }
                case EStepType._EStepTypeUnLockShakingScreen:
                {
                    Game.scene.gameControler.stopShake();
                    this.callEnd();
                    break;
                }
                case EStepType._EStepTypePreFallenToCopy:
                {
                    GameController.scene.cancelRoleCliff();
                    GameController.scene.RoleInSky();
                    this.callEnd();
                    break;
                }
                case EStepType._EStepTypeFallenToCopy:
                {
                    GameController.scene.RoleSlowDown();
                    setTimeout(this.callEnd, 3500);
                    break;
                }
                case EStepType._EStepTypeRoleBlowFly:
                {
                    this.blowFly.call(param1, this.callEnd);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function isCurStopable() : Boolean
        {
            if (this._lastStep == null)
            {
                return true;
            }
            switch(this._lastStep.type)
            {
                case EStepType._EStepTypeShakingScreen:
                case EStepType._EStepTypeUnLockShakingScreen:
                case EStepType._EStepTypePreFallenToCopy:
                case EStepType._EStepTypeFallenToCopy:
                {
                    return false;
                }
                default:
                {
                    return true;
                    break;
                }
            }
        }// end function

        public function resume(param1:Function) : void
        {
            var _loc_2:ITaskDramaStepCommand = null;
            this._resumedNum = 0;
            this._needResumeNum = 0;
            this._resumeCallback = param1;
            Cache.instance.task.isDrama = false;
            LayerManager.uiLayer.visible = true;
            unLockAll();
            if (this._runingSteper != null)
            {
                var _loc_3:String = this;
                var _loc_4:* = this._needResumeNum + 1;
                _loc_3._needResumeNum = _loc_4;
                this._runingSteper.cancel(new TTaskDrama(), this.resumeItemEnd);
                this._runingSteper = null;
            }
            for each (_loc_2 in this._curStepers)
            {
                
                var _loc_5:String = this;
                var _loc_6:* = this._needResumeNum + 1;
                _loc_5._needResumeNum = _loc_6;
                _loc_2.cancel(new TTaskDrama(), this.resumeItemEnd);
            }
            this._curStepers = new Dictionary();
            this.checkDeadTimer.stop();
            return;
        }// end function

        private function resumeItemEnd() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this._resumedNum + 1;
            _loc_1._resumedNum = _loc_2;
            if (this._resumedNum == this._needResumeNum)
            {
                if (this._resumeCallback != null)
                {
                    this._resumeCallback.apply();
                    this._resumeCallback = null;
                }
            }
            return;
        }// end function

        private function unlockThenEndTimeOut() : void
        {
            this._endTimerId = -1;
            this.callEndTimeOut();
            return;
        }// end function

        private function callEnd() : void
        {
            if (this._lastStep != null)
            {
                switch(this._lastStep.type)
                {
                    case EStepType._EStepTypeDramaPrint:
                    case EStepType._EStepTypeAnimationPrint:
                    case EStepType._EStepTypePlayerMoveScreen:
                    case EStepType._EStepTypeMoviePlay:
                    {
                        this._runingSteper = null;
                        break;
                    }
                    case EStepType._EStepTypeAnimationEnd:
                    {
                        delete this._curStepers[EStepType._EStepTypeAnimationEnd];
                        break;
                    }
                    case EStepType._EStepTypePlayerMoveOnlyDel:
                    {
                        delete this._curStepers[EStepType._EStepTypePlayerMoveOnly];
                        break;
                    }
                    case EStepType._EStepTypeScreenRestore:
                    {
                        delete this._curStepers[EStepType._EStepTypeScreenMove];
                        break;
                    }
                    case EStepType._EStepTypeUnLockMonsterScreenCenter:
                    {
                        delete this._curStepers[EStepType._EStepTypeMonsterScreenCenter];
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            this.callEndTimeOut();
            return;
        }// end function

        private function callEndTimeOut() : void
        {
            var nextFrame:Function;
            nextFrame = function () : void
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Task_DramaNextStep, _lastStep));
                return;
            }// end function
            ;
            this._endTimerId = -1;
            CallLater.addCallBack(nextFrame);
            return;
        }// end function

        private function get blackScreen() : TaskDramaOpBlackScreen
        {
            if (this._blackScreen == null)
            {
                this._blackScreen = new TaskDramaOpBlackScreen();
            }
            return this._blackScreen;
        }// end function

        private function get blackThenTips() : TaskDramaOpBlackScreenThenTips
        {
            if (this._blackThenTips == null)
            {
                this._blackThenTips = new TaskDramaOpBlackScreenThenTips();
            }
            return this._blackThenTips;
        }// end function

        private function get npcTalk() : TaskDramaOpTalk
        {
            if (this._npcTalk == null)
            {
                this._npcTalk = new TaskDramaOpTalk();
            }
            return this._npcTalk;
        }// end function

        private function get hidePet() : TaskDramaOpPetHide
        {
            if (this._hidePet == null)
            {
                this._hidePet = new TaskDramaOpPetHide();
            }
            return this._hidePet;
        }// end function

        private function get playerMoveOnly() : TaskDramaPlayerMoveOnly
        {
            if (this._playerMoveOnly == null)
            {
                this._playerMoveOnly = new TaskDramaPlayerMoveOnly();
            }
            return this._playerMoveOnly;
        }// end function

        private function get moveScreen() : TaskDramaScreenMove
        {
            if (this._moveScreen == null)
            {
                this._moveScreen = new TaskDramaScreenMove();
            }
            return this._moveScreen;
        }// end function

        private function get taskTalk() : TaskDramaOpTaskTalkText
        {
            if (this._taskTalk == null)
            {
                this._taskTalk = new TaskDramaOpTaskTalkText();
            }
            return this._taskTalk;
        }// end function

        private function get playerScreenMove() : TaskDramaPlayerScreenMove
        {
            if (this._playerScreenMove == null)
            {
                this._playerScreenMove = new TaskDramaPlayerScreenMove();
            }
            return this._playerScreenMove;
        }// end function

        private function get bossTalk() : TaskDramaBossTalk
        {
            if (this._bossTalk == null)
            {
                this._bossTalk = new TaskDramaBossTalk();
            }
            return this._bossTalk;
        }// end function

        private function get bossAction() : TaskDramaBossAction
        {
            if (this._bossAction == null)
            {
                this._bossAction = new TaskDramaBossAction();
            }
            return this._bossAction;
        }// end function

        private function get npcSceneTalk() : TaskDramaNpcTalk
        {
            if (this._npcSceneTalk == null)
            {
                this._npcSceneTalk = new TaskDramaNpcTalk();
            }
            return this._npcSceneTalk;
        }// end function

        private function get npcAction() : TaskDramaNpcAction
        {
            if (this._npcAction == null)
            {
                this._npcAction = new TaskDramaNpcAction();
            }
            return this._npcAction;
        }// end function

        private function get movie() : TaskDramaMovie
        {
            if (this._movie == null)
            {
                this._movie = new TaskDramaMovie();
            }
            return this._movie;
        }// end function

        private function get effect() : TaskDramaEffect
        {
            if (this._effect == null)
            {
                this._effect = new TaskDramaEffect();
            }
            return this._effect;
        }// end function

        private function get monsterScreen() : TaskDramaMonsterScreenCenter
        {
            if (this._monsterScreen == null)
            {
                this._monsterScreen = new TaskDramaMonsterScreenCenter();
            }
            return this._monsterScreen;
        }// end function

        private function get blowFly() : TaskDramaBlowFly
        {
            if (this._blowFly == null)
            {
                this._blowFly = new TaskDramaBlowFly();
            }
            return this._blowFly;
        }// end function

        public static function lockAll() : void
        {
            TaskDramaMouseKeyboardControl.enablePlayerOp = false;
            TaskDramaMouseKeyboardControl.enableViews = false;
            if (FuncPreviewModule.hasInstanceShowing())
            {
                FuncPreviewModule.instance.hideForDrama();
            }
            if (FriendBlessBall.hasInstanceShowing())
            {
                FriendBlessBall.instance.hideForDrama();
            }
            if (GameController.shopMall.isViewShow)
            {
                GameController.shopMall.view.hide();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.Wing288IconShowHide, false));
            GameController.guide.hideForDrama();
            return;
        }// end function

        public static function unLockAll() : void
        {
            TaskDramaMouseKeyboardControl.enablePlayerOp = true;
            TaskDramaMouseKeyboardControl.enableViews = true;
            if (FuncPreviewModule.hasInstanceShowing())
            {
                FuncPreviewModule.instance.hideForDrama(false);
            }
            if (FriendBlessBall.hasInstanceShowing())
            {
                FriendBlessBall.instance.hideForDrama(false);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.Wing288IconShowHide, true));
            GameController.guide.hideForDrama(false);
            return;
        }// end function

    }
}
